#!/usr/bin/env bash

set -e

init_centos()
{
    echo "Init centos"
    yum install -y python3 rsync git bash-completion wget curl
}

init_alpine()
{
    echo "Init alpine, todo ..."
    apk add python3 rsync bash-completion wget curl vim
}

init_arch()
{
    # pacman.conf
    echo "Init archlinux, todo ..."
    pacman -S --noconfirm bash-completion wget git rsync curl vim python3 tree flex bison
    pacman -S --noconfirm reflector
    reflector --sort rate --threads 100 -c China --save /etc/pacman.d/mirrorlist
    timedatectl set-timezone Asia/Shanghai
    timedatectl set-ntp true
}

clean_data()
{
    /bin/rm -rf ~/.local
    /bin/rm -rf ~/.ssh
    /bin/rm -rf ~/.gitconfig
    /bin/rm -rf ~/.aws
    sed -i '/tag_init/d' ~/.bashrc
}

if [ "$1" = "clean" ]; then
    clean_data
fi

dist="$(. /etc/os-release && echo "$ID")"
case $dist in
    centos|alios) init_centos ;;
    alpine)       init_alpine ;;
    arch)         init_arch ;;
esac

/usr/bin/rsync -a .local ~/
ln -s ~/.local/etc/clang-format ~/.clang-format
ln -s ~/.local/etc/gitconfig ~/.gitconfig
ln -s ~/.local/etc/gitignore ~/.gitignore
ln -s ~/.local/etc/config ~/.config
ln -s ~/.local/etc/aws ~/.aws
ln -s ~/.local/etc/aliyun ~/.aliyun

if grep -qi "microsoft" /proc/version || grep -qi "WSL2" /proc/version; then
    ln -s /mnt/c/Users/fjrti/ /root/fjrti
fi

if ! grep -q tag_init ~/.bashrc; then
     echo '[ -f ~/.local/etc/init.sh ] && source ~/.local/etc/init.sh # tag_init' >> ~/.bashrc
fi

sync; source ~/.bashrc
